﻿#include "Scene.h"
#include "Camera.h"
#include "EventHandler/TrackBallManipulator.h"
#include <qgl.h>
#include<QDebug>
namespace GCL {

Scene::Scene():camera_(new Camera()),manipulator_(new TrackBallManipulator(camera_.get())),
    has_light_(true),interval_(300)
{

}

/*void Scene::cubeTest()
{
    qDebug()<<"cubeTest";
}*/

Scene::~Scene()
{

}

void Scene::init()
{
    /// 背景色
    glClearColor(1.f,1.f,1.f,1.f);

    /// 打开深度测试： 如果关闭的话，物体就没有遮挡了;
    glEnable(GL_DEPTH_TEST);

    /// 关闭剔除面功能: 如果打开， 一些位于背面（基于当前相机视角）的面就不会渲染了；
    glDisable(GL_CULL_FACE);

    /// 打开颜色融合与设置融合函数：这样颜色的Alpha通道才会有作用
    glDisable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    this->setLighting(has_light_);
}

void Scene::render()
{
    glShadeModel(GL_SMOOTH);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    /// 光照
    if(has_light_)
    {
        this->renderLights();
    }
    /// 相机
    camera_->enter();
        /// 具体的渲染
        this->renderObjects();
    camera_->leave();
}

void Scene::timerRun()
{

}

Camera *Scene::getCamera()
{
    return camera_.get();
}

void Scene::enableManipulator(bool t)
{
    manipulator_->setEnable(t);
}

void Scene::setManipulator(const std::shared_ptr<Manipulator> &manipulator)
{
    manipulator_ = manipulator;
}

void Scene::setLighting(bool t)
{
    has_light_ = t;
    if(has_light_)
    {
        ///* 打开光照
        glEnable(GL_LIGHTING);
        ///* 打开颜色材质
        glEnable(GL_COLOR_MATERIAL);
        ///* 打开双面光照
        glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);

    }
    else
    {
        ///* 关闭光照
        glDisable(GL_LIGHTING);
        ///* 关闭颜色材质
        glDisable(GL_COLOR_MATERIAL);
    }
}

void Scene::renderLights()
{
    // Enable two OpenGL lights
    GLfloat light_diffuse[]   = { .8f,  .8f,  .8f,  1.0f};  // 白光
    GLfloat light_position0[] = {-1.0f, -1.0f, -1.0f,  0.0f};  // Infinite light location
    GLfloat light_position1[] = { 1.0f,  1.0f,  1.0f,  0.0f};  // Infinite light location

    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position0);
    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
    glLightfv(GL_LIGHT1, GL_POSITION, light_position1);
    glEnable(GL_LIGHT1);
}
int Scene::interval() const
{
    return interval_;
}

void Scene::setInterval(int interval)
{
    interval_ = interval;
}


void Scene::resize(int w, int h)
{
    ///* 相机视角需要改变
    camera_->set_viewport(w,h);

    if(manipulator_->isEnable())
    {
        manipulator_->resize(w,h);
    }
}

void Scene::mouseMove(int posX, int posY)
{
    if(manipulator_->isEnable())
    {
        manipulator_->mouseMove(posX,posY);
    }
}

void Scene::mousePress(int posX, int posY, unsigned int button, unsigned int modifier)
{
    if(manipulator_->isEnable())
    {
        manipulator_->mousePress(posX,posY,button,modifier);
    }
}

void Scene::mouseRelease(int posX, int posY, unsigned int button, unsigned int modifier)
{
    if(manipulator_->isEnable())
    {
        manipulator_->mouseRelease(posX,posY,button,modifier);
    }
}

void Scene::keyUp(const unsigned int &key, unsigned int modifier)
{
    if(manipulator_->isEnable())
    {
        manipulator_->keyUp(key,modifier);
    }
}

void Scene::keyDown(const unsigned int &key, unsigned int modifier)
{
    if(manipulator_->isEnable())
    {
        manipulator_->keyDown(key,modifier);
    }
}

void Scene::wheel(const float &delta)
{
    if(manipulator_->isEnable())
    {
        manipulator_->wheel(delta);
    }
}



}
